package helpdesk;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
 *
 * @author MattLampe
 */
public class ViewUsersGUI extends javax.swing.JFrame
{
    ArrayList<User> data;
    String collection;
    
    /**
     * Creates new form ViewDocGUI
     */
    public ViewUsersGUI(ArrayList<User> userList, String collect)
    {
        
        data = userList;
        collection = collect;
        initComponents();
        //this views the users and their roles and displays them in a table
        docTable.getColumnModel().getColumn(0).setHeaderValue("User");
        docTable.getColumnModel().getColumn(1).setHeaderValue("Role");
        
        User temp;
        
        for(int i = 0; i < data.size(); i++)
        {
            temp = data.get(i);
            docTable.setValueAt(temp.username, i, 0);
            docTable.setValueAt(temp.role, i, 1);
        }
        setVisible(true);
    }

    private ViewUsersGUI()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        docTable = new javax.swing.JTable();
        promoteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        demoteButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
        jLabel1.setText("Current Users");

        docTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return data.size();
                }

                @Override
                public int getColumnCount()
                {
                    return 2;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    if (j == 0)
                    {
                        return data.get(i).username;
                    }
                    else
                    {
                        return data.get(i).role;
                    }
                }

                public void setValueAt(String val, int i, int j)
                {
                    User jim = data.get(i);
                    data.set(i, jim);
                }
            });
            jScrollPane1.setViewportView(docTable);

            promoteButton.setText("Promote");
            promoteButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    promoteButtonActionPerformed(evt);
                }
            });

            cancelButton.setText("Cancel");
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cancelButtonActionPerformed(evt);
                }
            });

            demoteButton.setText("Demote");
            demoteButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    demoteButtonActionPerformed(evt);
                }
            });

            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 243, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(layout.createSequentialGroup()
                                    .add(38, 38, 38)
                                    .add(jLabel1)))
                            .addContainerGap())
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(0, 0, Short.MAX_VALUE)
                            .add(promoteButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(demoteButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(cancelButton)
                            .add(14, 14, 14))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .add(18, 18, 18)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(promoteButton)
                        .add(cancelButton)
                        .add(demoteButton))
                    .addContainerGap(21, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents
    //This button promotes the selected user to the next higher role
    private void promoteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_promoteButtonActionPerformed
        if (docTable.getSelectedRow() >= 0)
        {
            MongoDB.changeRole(data.get(docTable.getSelectedRow()).username, "Graduate Assistant");
            this.setVisible(false);
            new ViewUsersGUI(MongoDB.getPotentialUsers(), "users");
            this.dispose();
        }
    }//GEN-LAST:event_promoteButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed
    //This button demotes the selected user to the next lowest role
    private void demoteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_demoteButtonActionPerformed
        if (docTable.getSelectedRow() >= 0)
        {
            MongoDB.changeRole(data.get(docTable.getSelectedRow()).username, "Student");
            this.setVisible(false);
            new ViewUsersGUI(MongoDB.getPotentialUsers(), "users");
            this.dispose();
        }
    }//GEN-LAST:event_demoteButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewUsersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewUsersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewUsersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewUsersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewUsersGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton demoteButton;
    private javax.swing.JTable docTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton promoteButton;
    // End of variables declaration//GEN-END:variables
}
